#ifndef __HTTPREQUEST_H__
#define __HTTPREQUEST_H__

#include <string>
#include <unordered_map>
#include <sstream>

using std::string;
using std::unordered_map;

enum class HttpMethod {
    GET,
    POST,
    PUT,
    DELETE,
    HEAD,
    OPTIONS,
    UNKNOWN
};

class HttpRequest {
public:
    HttpRequest();
    ~HttpRequest();
    
    // 解析HTTP请求
    bool parseRequest(const string &rawRequest);
    
    // 获取请求信息
    HttpMethod getMethod() const { return _method; }
    const string& getPath() const { return _path; }
    const string& getVersion() const { return _version; }
    const string& getBody() const { return _body; }
    
    // 获取请求头
    string getHeader(const string &name) const;
    bool hasHeader(const string &name) const;
    const unordered_map<string, string>& getAllHeaders() const { return _headers; }
    
    // 获取查询参数
    string getQuery(const string &name) const;
    bool hasQuery(const string &name) const;
    const unordered_map<string, string>& getAllQueries() const { return _queries; }
    
    // 工具方法
    string getMethodString() const;
    bool isComplete() const { return _isComplete; }
    void reset();
    
    // 调试输出
    string toString() const;

private:
    bool parseRequestLine(const string &line);
    bool parseHeaders(const string &headerSection);
    void parseQueryString(const string &queryString);
    HttpMethod stringToMethod(const string &methodStr);

private:
    HttpMethod _method;
    string _path;
    string _version;
    string _body;
    unordered_map<string, string> _headers;
    unordered_map<string, string> _queries;
    bool _isComplete;
    size_t _contentLength;
};

#endif